package xworker.gluon.glisten.mvc;

import com.gluonhq.charm.glisten.mvc.View;
import org.xmeta.ActionContext;
import org.xmeta.Thing;
import xworker.javafx.beans.property.PropertyFactory;
import xworker.javafx.scene.layout.BorderPaneActions;

public class ViewActions {
    static{
        PropertyFactory.regist(View.class, "showTransitionFactoryProperty", o -> {
            View obj = (View) o;
            return obj.showTransitionFactoryProperty();
        });
        PropertyFactory.regist(View.class, "onCloseRequestProperty", o -> {
            View obj = (View) o;
            return obj.onCloseRequestProperty();
        });
        PropertyFactory.regist(View.class, "onShownProperty", o -> {
            View obj = (View) o;
            return obj.onShownProperty();
        });
        PropertyFactory.regist(View.class, "onShowingProperty", o -> {
            View obj = (View) o;
            return obj.onShowingProperty();
        });
        PropertyFactory.regist(View.class, "onHiddenProperty", o -> {
            View obj = (View) o;
            return obj.onHiddenProperty();
        });
        PropertyFactory.regist(View.class, "onHidingProperty", o -> {
            View obj = (View) o;
            return obj.onHidingProperty();
        });
    }

    public static void init(View view, Thing thing, ActionContext actionContext){
        BorderPaneActions.init(view, thing, actionContext);
    }

    public static Object create(ActionContext actionContext){
        Thing self = actionContext.getObject("self");

        View view = new ThingView(self, actionContext);
        init(view, self, actionContext);
        actionContext.g().put(self.getMetadata().getName(), view);

        actionContext.peek().put("parent", view);
        for(Thing child : self.getChilds()){
            child.doAction("create", actionContext);
        }

        return view;
    }
}
